<?php

    /**
     * 获取一个数据库连接
     * @return object 返回一个mysqli的连接对象
     */
    function getConnection(){
        $servername = "localhost:3306";
        $username = "root";
        $password = "123456";
        $dbname = "petshop";

        $conn = new mysqli($servername, $username, $password, $dbname);

        if($conn->connect_error){
            die("连接失败:". $conn->connect_error);
        }

        return $conn;
    }

    /**
     * 获取前端json格式的参数
     * @return array 返回一个键值对数组对象, key为前端传过来的参数
     */
    function getJsonParamter(){
        $post = file_get_contents('php://input');
        $post =  json_decode( $post, true );

        return $post;
    }
    
    /**
     * 执行数据库增删改操作函数
     * @param object 数据库连接对象
     * @param string sql语句
     * @param array 预处理参数对象
     * 
     * @return boolean  成功返回true 失败返回false
    */
    function execute($conn, string $sql, ...$vars){

        $stmt = $conn->stmt_init();

        $stmt->prepare($sql);

        $types = getTypes(...$vars);

        $stmt->bind_param($types, ...$vars);

        $stmt->execute();

        $result = hasChange($conn);

        // if(!$result){
        //     printf("Error: %s.\n", $stmt->error);
        // }

        $stmt->close();

        return $result;
    }

    /**
     * 获取PHP类型在mysqli中类型的格式
     * 
     * @param array 参数列表
     *
     * @return string 返回mysqli的类型格式
     */
    function getTypes(...$vars){
        $result = "";
        foreach($vars as $v){
            $type = gettype($v);

            switch($type){
                case 'integer':
                    $result .= "i";
                    break;
                case 'double':
                    $result .= "d";
                    break;
                case 'string':
                    $result .= "s";
                    break;
                case 'boolean':
                    $result .= "b";
                    break;
            }
        }

        return $result;
    }


    /**
     * 数据库查询单个结果函数
     * 
     *  @param object 数据库连接对象
     * 
     *  @param string sql语句
     * 
     *  @param array 预处理参数
     * 
     * @return 返回查询到的结果
     * */
    function query($conn, string $sql, ...$vars){
        $stmt = $conn->stmt_init();

        $stmt->prepare($sql);

        if($vars != null){
            $types = getTypes(...$vars);
            $stmt->bind_param($types, ...$vars);
        }

        $stmt->execute();

        $result = $stmt->result_metadata();

        $array = array();

        while($field = $result -> fetch_field()){
            $array[$field->name] = null;
        }

        $values = array_values($array);
        $keys = array_keys($array);

        $stmt->bind_result(...$values);
        $stmt->fetch();

        $null_data = true;

        foreach($values as $val){
            if($val != null){
                $null_data = false;
                break;
            }
        }

        if($null_data)
            return null;

        for($i = 0; $i < count($keys); $i++){
            $array[$keys[$i]] = $values[$i];
        }

        return $array;

    }

    /**
     * 数据库查询结果集函数
     * 
     *@param object 数据库连接对象
     * 
     *@param string sql语句
     * 
     *@param array 预处理参数
     * 
     *@return 返回查询到的结果集
     * */
    function query_list($conn, string $sql, ...$vars){

        $types = getTypes(...$vars);

        $stmt = $conn->stmt_init();

        $stmt->prepare($sql);

        if($types != null && $vars != null){
            $stmt->bind_param($types, ...$vars);
        }

        $stmt->execute();

        $result = $stmt->result_metadata();

        $obj = array();
        $array = array();

        while($field = $result -> fetch_field()){
            $obj[$field->name] = $field->name;
        }

        $values = array_values($obj);
        $keys = array_keys($obj);

        $stmt->bind_result(...$values);


        while($stmt->fetch()){

            $null_data = true;

            foreach($values as $val){
                if($val != null){
                    $null_data = false;
                    break;
                }
            }
    
            if($null_data){
                array_push($array, null);
                continue;
            }

            for($i = 0; $i < count($keys); $i++){
                $obj[$keys[$i]] = $values[$i];
            }

            array_push($array, $obj);
        }

        return $array;
    }


    /**
     * 查看表是否受到影响
     * 
     * @param object    数据库连接对象
     * 
     * @return 受到影响返回true,否则返回false
     */
    function hasChange($conn){
        if(mysqli_affected_rows($conn) <= 0)
            return false;

        return true;
    }

    /**
     * 返回json格式数据
     * 
     * @param code   响应状态: 200为正常 500为内部错误
     * @param msg 响应消息
     * @param data 响应数据
     */
    function getJson($code, $msg, $data){
        $obj = array();

        $obj['code'] = $code;
        $obj['msg'] = $msg;
        $obj['data'] = $data;

        echo json_encode($obj);
    }

    /**
     * 判断请求方式是否是get
     * 
     * @return 是get请求返回true,否则返回false
     */
    function is_get(){
    return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
}


    /**
     * 判断请求方式是否是post
     * 
     * @return 是post请求返回true,否则返回false
     */
    function is_post(){
        return $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false;
    }

?>